home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / strings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  4.1 KB  |  241 lines

  1. /*
  2.  * Miscellaneous string routines.
  3.  */
  4.  
  5. #define STRSTR
  6.  
  7. #include <stdio.h>
  8. #include "config.h"
  9.  
  10. /*
  11.  * Do a fancy string copy.  If NULL, return null.  If pointer to NULL, then
  12.  * return the special "null_ptr" variable.  If a normal copy, allocate
  13.  * memory first.
  14.  */
  15.  
  16. char *
  17. str_dup(str)
  18. char *str;
  19. {
  20.     extern char *null_ptr;
  21.     char *ret, *malloc(), *strcpy();
  22.     void error_win();
  23.  
  24.     if (str == NULL)
  25.         return(NULL);
  26.                     /* if pointer to null */
  27.     if (*str == '\0')
  28.         return(null_ptr);
  29.  
  30.     if (!(ret = malloc((unsigned int) strlen(str)+1)))
  31.         error_win(1, "Out of memory", "");
  32.  
  33.     strcpy(ret, str);
  34.     return(ret);
  35. }
  36.  
  37. /*
  38.  * Perform the free(2) function, but check for NULL and the special
  39.  * "null_ptr" variable first.
  40.  */
  41.  
  42. void
  43. free_ptr(str)
  44. char *str;
  45. {
  46.     extern char *null_ptr;
  47.     void free();
  48.  
  49.     if (str != NULL && str != null_ptr)
  50.         free(str);
  51.     return;
  52. }
  53.  
  54. /*
  55.  * Replace a string.  Follows the same convention as str_dup(), except
  56.  * that realloc() is used instead of malloc().  Returns a pointer to
  57.  * the new string (which may have moved).
  58.  */
  59.  
  60. char *
  61. str_rep(s1, s2)
  62. char *s1, *s2;
  63. {
  64.     extern char *null_ptr;
  65.     void free_ptr(), error_win();
  66.     char *s, *malloc(), *realloc(), *strcpy();
  67.  
  68.                     /* copy null pointer ? */
  69.     if (s2 == NULL) {
  70.         free_ptr(s1);
  71.         return(NULL);
  72.     }
  73.     if (s2 == '\0') {
  74.         free_ptr(s1);
  75.         return(null_ptr);
  76.     }
  77.                     /* use realloc()? */
  78.     if (s1 == NULL || s1 == null_ptr) {
  79.         if (!(s = malloc((unsigned int) strlen(s2)+1)))
  80.             error_win(1, "Out of memory", "");
  81.     }
  82.     else {
  83.         if (!(s = realloc(s1, (unsigned int) strlen(s2)+1)))
  84.             error_win(1, "Out of memory", "");
  85.     }
  86.  
  87.     strcpy(s, s2);
  88.     return(s);
  89. }
  90.  
  91. /*
  92.  * This routine is similar to strtok(3).  But this version handles missing
  93.  * tokens by returning a pointer to null.  Also it takes a single separator
  94.  * character as an argument.  Returns a NULL on end of string or error.
  95.  */
  96.  
  97. char *
  98. str_tok(str, c)
  99. char *str, c;
  100. {
  101.     char *strchr();
  102.     static char *ptr, *sep;
  103.                     /* start at beginning */
  104.     if (str != NULL)
  105.         ptr = str;
  106.     else
  107.         ptr = sep;
  108.                     /* at the end? */
  109.     if (*ptr == '\0')
  110.         return(NULL);
  111.                     /* no separator? */
  112.     if (!(sep = strchr(ptr, c)))
  113.         return(NULL);
  114.                     /* zap the sep, move past it */
  115.     *sep = '\0';
  116.     sep++;
  117.  
  118.     return(ptr);
  119. }
  120.  
  121. #ifdef STRSTR
  122. /*
  123.  * Return a pointer to the first occurrence of string str2 in str1.
  124.  * Returns a NULL if str2 is not in str1.
  125.  */
  126.  
  127. char *
  128. strstr(str1, str2)
  129. char *str1, *str2;
  130. {
  131.     int len;
  132.  
  133.     len = strlen(str2);
  134.     while (*str1) {
  135.         if (*str2 == *str1) {
  136.             if (!strncmp(str2, str1, len))
  137.                 return(str1);
  138.         }
  139.         str1++;
  140.     }
  141.     return(NULL);
  142. }
  143. #endif /* STRSTR */
  144.  
  145. #ifdef BSD
  146. /*
  147.  * Returns the length of the initial segment of string which consists
  148.  * entirely of characters from charset.
  149.  */
  150.  
  151. int
  152. strspn(string, charset)
  153. char    *string;
  154. register char    *charset;
  155. {
  156.     register char *p, *q;
  157.  
  158.     for(q=string; *q != '\0'; ++q) {
  159.         for(p=charset; *p != '\0' && *p != *q; ++p)
  160.             ;
  161.         if(*p == '\0')
  162.             break;
  163.     }
  164.     return(q-string);
  165. }
  166.  
  167. /*
  168.  * Strtok considers string to consist of a sequence of zero or more
  169.  * text tokens separated by spans of one or more characters from sepset.
  170.  */
  171.  
  172. char *
  173. strtok(string, sepset)
  174. char    *string, *sepset;
  175. {
  176.     register char    *p, *q, *r;
  177.     static char    *savept;
  178.     char *strpbrk();
  179.  
  180.     /*first or subsequent call*/
  181.     p = (string == NULL)? savept: string;
  182.  
  183.     if(p == 0)        /* return if no tokens remaining */
  184.         return(NULL);
  185.  
  186.     q = p + strspn(p, sepset);    /* skip leading separators */
  187.  
  188.     if(*q == '\0')        /* return if no tokens remaining */
  189.         return(NULL);
  190.  
  191.     if((r = strpbrk(q, sepset)) == NULL)    /* move past token */
  192.         savept = 0;    /* indicate this is last token */
  193.     else {
  194.         *r = '\0';
  195.         savept = ++r;
  196.     }
  197.     return(q);
  198. }
  199.  
  200. /*
  201.  * Return ptr to first occurrence of any character from `brkset'
  202.  * in the character string `string'; NULL if none exists.
  203.  */
  204.  
  205. char *
  206. strpbrk(string, brkset)
  207. register char *string, *brkset;
  208. {
  209.     register char *p;
  210.  
  211.     if(!string || !brkset)
  212.         return(0);
  213.     do {
  214.         for(p=brkset; *p != '\0' && *p != *string; ++p)
  215.             ;
  216.         if(*p != '\0')
  217.             return(string);
  218.     }
  219.     while(*string++);
  220.     return(0);
  221. }
  222.  
  223. /*
  224.  * Copies the character c, n times to string s
  225.  */
  226.  
  227. /* char *
  228. memset(s, c, n)
  229. char *s, c;
  230. int n;
  231. {
  232.     char *s1 = s;
  233.  
  234.     while (n > 0) {
  235.         --n;
  236.         *s++ = c;
  237.     }
  238.     return(s1);
  239. }   */
  240. #endif /* BSD */
  241.